package com.nine.gxh.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nine.gxh.dtos.GxhInfoChannelDto;
import com.nine.gxh.model.GxhInfoChannel;
import com.nine.gxh.service.GxhInfoChannelService;
import com.nine.gxh.vos.GxhInfoChannelVo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author: duonine
 * @date: 2023/7/3
 */
@RestController
@RequestMapping("/gxh/channel")
public class GxhInfoChannelController {

    @Resource
    private GxhInfoChannelService gxhInfoChannelService;

    @PostMapping("/list")
    public AjaxResult listAll(@RequestBody GxhInfoChannelDto dto) {
        IPage<GxhInfoChannel> page = gxhInfoChannelService.listAll(dto);
        return AjaxResult.success(page);
    }

    @PostMapping("/list/xml")
    public AjaxResult listByXml(@RequestBody GxhInfoChannelDto dto) {
        List<GxhInfoChannel> list = gxhInfoChannelService.listByXml(dto);
        return AjaxResult.success(list);
    }

    @PostMapping("/add")
    public AjaxResult addChannel(@RequestBody GxhInfoChannelDto dto) {
        GxhInfoChannel channel = gxhInfoChannelService.addChannel(dto);
        Map<String, Object> map = new HashedMap<>();
        map.put("channelId", channel.getId().toString());
        return AjaxResult.success(map);
    }

    @PostMapping("/del/{channelId}")
    public AjaxResult delChannel(@PathVariable(value = "channelId") String channelId) {
        boolean flag = gxhInfoChannelService.delChannel(channelId);
        return AjaxResult.success(flag);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/del")
    public AjaxResult delChannelBatch(@RequestBody List<String> ids) {
        boolean flag = gxhInfoChannelService.delChannelBatch(ids);
        return AjaxResult.success(flag);
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @PostMapping("/update")
    public AjaxResult updateChannel(@RequestBody GxhInfoChannelDto dto) {
        GxhInfoChannel channel = gxhInfoChannelService.updateChannel(dto);
        Map<String, Object> map = new HashedMap<>();
        map.put("channelId", channel.getId().toString());
        return AjaxResult.success(map);
    }

    /**
     * 详情
     *
     * @param channelId
     * @return
     */
    @PostMapping("/detail/{channelId}")
    public AjaxResult detailChannel(@PathVariable(value = "channelId") String channelId) {
        GxhInfoChannel channel = gxhInfoChannelService.detailChannel(channelId);
        Map<String, Object> map = new HashedMap<>();
        map.put("channel", channel);
        return AjaxResult.success(map);
    }

    /**
     * 导出
     *
     * @param response
     * @param channelVo
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, GxhInfoChannelVo channelVo) {
        List<GxhInfoChannel> list = gxhInfoChannelService.exportList(channelVo);
        ExcelUtil<GxhInfoChannel> util = new ExcelUtil<>(GxhInfoChannel.class);
        util.exportExcel(response, list, "测试导出数据");
    }

    /**
     * 导出【easyExcel】
     *
     * @param response
     * @param channelVo
     */
    @PostMapping("/export/easyexcel")
    public void export2(HttpServletResponse response, GxhInfoChannelVo channelVo) {
        List<GxhInfoChannel> list = gxhInfoChannelService.exportList(channelVo);
        ExcelUtil<GxhInfoChannel> util = new ExcelUtil<>(GxhInfoChannel.class);
        util.exportEasyExcel(response, list, "测试导出数据");
    }
}
